//微信授权登录
const wxLogin = () => {
	return new Promise((resolve) => {
		uni.getProvider({
			service: 'oauth',
			success: function(res) {
				if (~res.provider.indexOf('weixin')) {
					uni.login({
						provider: 'weixin',
						success: (res2) => {
							const wxcode = res2.code
							resolve({
								code: 0,
								json: {
									code: wxcode
								}
							});
						},
						fail: () => {
							uni.showToast({
								title: "微信登录授权失败,网络异常",
								icon: "none"
							});
							resolve({
								code: 2,
								json: "微信登录授权失败,网络异常"
							});
						}
					})

				} else {
					uni.showToast({
						title: '请先安装微信或升级版本',
						icon: "none"
					});
					resolve({
						code: 3,
						json: "请先安装微信或升级版本"
					});
				}
			}
		});
	})
}

// 获取坐标
const getLocation = function() {
	return new Promise((resolve) => {
		uni.getLocation({
			// type: 'wgs84',
			type: 'gcj02',
			success(res) {
				const latitude = res.latitude
				const longitude = res.longitude
				const speed = res.speed
				const accuracy = res.accuracy
				resolve({
					code: 0,
					json: {
						latitude,
						longitude,
						speed,
						accuracy
					}
				})
			},
			fail() {
				resolve({
					code: 1,
					json: '您已取消位置授权，需要手动选择位置。'
				})
			},
		})
	})
}
//判断位置权限
const hasLocation = function() {
	//第一次小程序会弹出一个授权框，拒绝之后不会再次弹出，直接进入fail回调，需引导用户进入设置页面打开授权
	// 利用promise
	return new Promise((resolve) => {
		uni.getSetting({
			success: (res) => {
				//未授权
				if (res.authSetting['scope.userLocation'] == undefined) {
					resolve({
						code: 1,
						json: '位置未授权'
					})
				}
				//已授权：拒绝
				else if (res.authSetting['scope.userLocation'] != undefined && res.authSetting[
						'scope.userLocation'] != true) {
					resolve({
						code: 2,
						json: '您已取消位置授权'
					})
				}
				//已授权：同意
				else {
					resolve({
						code: 0,
						json: '位置已授权'
					})
				}
			},
			fail: (error) => {
				resolve({
					code: 3,
					json: error
				})
			},
		})
	})
}

//设置系统配置
const setOpenSetting = function() {
	return new Promise((resolve) => {
		uni.showModal({
			title: '温馨提示',
			content: '小程序需要获取您的位置授权，用于显示当前站点',
			success: res => {
				if (res.confirm) {
					uni.openSetting({
						success: (res) => {
							resolve({
								code: 0,
								json: res
							})
						},
						fail: (error) => {
							resolve({
								code: 1,
								json: error
							})
						}
					})
				} else if (res.cancel) {
					resolve({
						code: 1,
						json: '用户点击取消'
					})
				}
			}
		})
	})
}

//检查位置授权+引导权限设置
const checkLocation = async function() {
	let res = await hasLocation();
	if (res.code == 0) {
		//已授权,获取坐标
		return true;
	} else if (res.code == 2 || res.code == 1) {
		//已拒绝授权,打开设置面板
		wx.hideLoading();
		let res = await setOpenSetting();
		if (res.code == 0) return true;
		else return false;
	} else if (res.code == 3) {
		//查询配置失败
		wx.showToast({
			title: res.json,
			icon: 'none',
			duration: 1000,
		})
		return false;
	}
}

//仅检查位置授权
const checkLocationOnly = async function() {
	let res = await hasLocation();
	if (res.code == 0) {
		//已授权,获取坐标
		return true;
	} else if (res.code == 2 || res.code == 1) {
		//已拒绝授权,打开设置面板
		return false;
	} else if (res.code == 3) {
		//查询配置失败
		return false;
	}
}

module.exports = {
	wxLogin,
	getLocation,
	hasLocation,
	setOpenSetting,
	checkLocation,
	checkLocationOnly
}